package com.app.miwawa.api.eam.service.bo;

import org.apache.commons.lang3.StringUtils;
import org.framework.common.api.service.bo.CommonBoAdapter;
import org.framework.common.api.support.constant.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.app.miwawa.api.eam.entity.ClassesStudent;
import com.app.miwawa.api.eam.service.dao.ClassesStudentDao;

@Service("classesStudentBo")
public class ClassesStudentBo extends CommonBoAdapter<ClassesStudent, String> {

	@Autowired
	private ClassesStudentDao dao;
	
	@Autowired
	private JdbcTemplate jdbc;
	
	/**
	 * 逻辑删除班级学员表中的纪录.
	 * PS:分班去重
	 * @param ids
	 * @author chanlong(陈龙)
	 * @param classesids 
	 * @date 2016年4月14日 下午1:30:18
	 */
	public void remove(final String studentids, final String classesids) throws Exception {
		if(StringUtils.isNotEmpty(studentids) && StringUtils.isNotEmpty(classesids)){
			dao.remove(StringUtils.split(studentids, SystemConstants.SYMBOL_COMMA), StringUtils.split(classesids, SystemConstants.SYMBOL_COMMA));
		}
	}
	
	/**
	 * 更新班级信息时，同时更新班级学员表的班级名称.
	 * @param classes
	 * @param classesid
	 * @throws Exception
	 * @author chanlong(陈龙)
	 * @date 2016年4月16日 下午1:25:55
	 */
	public void update(final String classes, final String classesid) throws Exception {
		if(StringUtils.isNotEmpty(classes) && StringUtils.isNotEmpty(classesid)){
			dao.update(classes, classesid);
			
			jdbc.execute(getSQL("tb_eam_student_check", classes, classesid));
			jdbc.execute(getSQL("tb_eam_student_leave", classes, classesid));
		}
	}
	
	private String getSQL(final String table, final String classes, final String classesid){
		String sql = "update table set classes = ':classes' where classes_id = ':classesid'";
		return sql.replace("table", table).replaceFirst(":classes", classes).replaceFirst(":classesid", classesid);
	}
}
